package com.woniuxy.microgoldservice.utils;

import com.woniuxy.microgoldservice.bean.GoldPriceDTO;
import com.woniuxy.microgoldservice.bean.WorkDayOutBean;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author hongbin
 * @date 2021-04-21 17:01
 * Description：判断每年的周末
 */

public class DateUtils {

    @Resource
    private RestTemplate restTemplate;

    //判断闰年
    public static boolean judgeLeapYear(int year) {
        if (year % 400 == 0) {
            return true;
        } else if (year % 4 == 0 && year % 100 != 0) {
            return true;
        } else {
            return false;
        }
    }

    //判断周末
    public static List<String> judgeWorkDay(int year) {
        ArrayList<String> workDays = new ArrayList<>();

        boolean b = judgeLeapYear(year);

        int monthDay = 28;
        if (b) {
            monthDay = 29;
        }

        Calendar ins = Calendar.getInstance();
        for (int i = 1; i <= 12; i++) {

            ins.set(Calendar.MONTH, i - 1);
            if (i % 2 == 0 || i == 7) {
                if (i == 2) {
                    for (int j = 1; j <= monthDay; j++) {
                        ins.set(Calendar.DAY_OF_MONTH, j);
                        if (ins.get(Calendar.DAY_OF_WEEK) == 1 || ins.get(Calendar.DAY_OF_WEEK) == 7) {
                            workDays.add(ins.get(Calendar.YEAR) + "-" +
                                    String.format("%02d", (ins.get(Calendar.MONTH) + 1)) + "-" +
                                    String.format("%02d", ins.get(Calendar.DAY_OF_MONTH)));
                        }
                    }
                } else {
                    for (int j = 1; j <= 31; j++) {
                        ins.set(Calendar.DAY_OF_MONTH, j);
                        if (ins.get(Calendar.DAY_OF_WEEK) == 1 || ins.get(Calendar.DAY_OF_WEEK) == 7) {

                            workDays.add(ins.get(Calendar.YEAR) + "-" +
                                    String.format("%02d", (ins.get(Calendar.MONTH) + 1)) + "-" +
                                    String.format("%02d", ins.get(Calendar.DAY_OF_MONTH)));
                        }
                    }
                }
            } else {

                for (int j = 1; j <= 30; j++) {
                    ins.set(Calendar.DAY_OF_MONTH, j);
                    if (ins.get(Calendar.DAY_OF_WEEK) == 1 || ins.get(Calendar.DAY_OF_WEEK) == 7) {

                        workDays.add(ins.get(Calendar.YEAR) + "-" +
                                String.format("%02d", (ins.get(Calendar.MONTH) + 1)) + "-" +
                                String.format("%02d", ins.get(Calendar.DAY_OF_MONTH)));
                    }
                }

            }

        }

        return workDays;
    }

    //判断交易日
    public static boolean judgeDealDay(Integer startTime,Integer startHour,Integer endTime,Integer endHour){

        Date date = new Date();
        String day = new SimpleDateFormat("dd").format(date);
        String hour = new SimpleDateFormat("HH").format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
//        calendar.set(Calendar.DAY_OF_MONTH,25);
        int i = calendar.get(Calendar.DAY_OF_WEEK)-1;
        if (i==0){
            i=7;
        }

//        Integer startTime =1;
//        Integer startHour = 9;
//        Integer endTime = 6;
//        Integer endHour = 0;
        //获取交易日
        ArrayList<Integer> list = new ArrayList<>();
        for (int j = startTime; j <= endTime; j++) {
            list.add(j);
        }
        //获取非交易日
        ArrayList<Integer> outSideTime = new ArrayList<>();
        for (int j = 1; j <8 ; j++) {
            if(list.contains(j)){
                continue;
            }
            outSideTime.add(j);
        }
        //判断是否为交易时间
        if(outSideTime.contains(i)){
            return false;
        }else{
            int i1 = Integer.parseInt(hour);
            if (i==startTime && i1<startHour){
                return false;
            }else if(i==endTime && i1>=endHour){
                return false;
            }else{
                return true;
            }
        }
//        System.out.println("今天是星期："+i);
//        System.out.println("list:"+list);
//        System.out.println("outSideTime:"+outSideTime);
//        System.out.println("status:"+dto.getStatus());

    }

    public WorkDayOutBean isHoliday(){
        WorkDayOutBean forObject = restTemplate.getForObject("https://api.apihubs.cn/holiday/get", WorkDayOutBean.class);
        System.out.println(forObject);
        return forObject;
    }
}
